package com.example.server;

import com.example.client.Client;
import com.example.client.ClientData;
import com.example.util.iotDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class ServerLog {

    public static ObservableList<ClientData> clientDataList = FXCollections.observableArrayList();

    public ServerLog(){
        //增加几组数据:初始数据
//        clientDataList.add(new ClientData("2022-9-1","20:27:10","30.2","46","480","362"));
//        clientDataList.add(new ClientData("2022-9-2","19:13:09","28.9","53","370","490"));
//        clientDataList.add(new ClientData("2022-9-3","23:56:13","25.0","61","432","387"));
    }

    public ObservableList<ClientData> getData(){
        return clientDataList;
    }

    public void showAllData() throws IOException {
        System.out.println("showAllData");
        FXMLLoader fxmlLoader = new FXMLLoader(Server.class.getResource("ServerLog.fxml"));
        Scene scene = new Scene(fxmlLoader.load(),947,603);
        Stage stage = new Stage();
        //stage.getIcons().add(new Image(Client.class.getResource("logoData.png").toExternalForm()));
        stage.setTitle("采集端数据");
        stage.setScene(scene);
        stage.show();

        //给Controller控制该类的权限
        ServerLogController serverLogController = fxmlLoader.getController();
        serverLogController.setServerLog(this);
    }

    public void showList(String address) throws IOException, SQLException {

        //清空列表
        clientDataList = FXCollections.observableArrayList();
        clientDataList.clear();
        //从数据库获取地点数据表单并显示在左边tableView中
        iotDao iotDao = new iotDao();

        ResultSet rs = iotDao.searchData(address);
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        System.out.println("rs row =" + row);

        ResultSetMetaData rsmd = rs.getMetaData();
        while (rs.next()) {
            ClientData clientData = new ClientData();

            for (int j = 1; j <= 7; j++) {
                //if (j > 1) System.out.print(",  \n");
                String columnValue = rs.getString(j);
                switch(j%7){
                    case 2:
                        clientData.setDate(columnValue);
                        break;
                    case 3:
                        clientData.setTime(columnValue);
                        break;
                    case 4:
                        clientData.setTemperature(columnValue);
                        break;
                    case 5:
                        clientData.setHumidity(columnValue);
                        break;
                    case 6:
                        clientData.setCo2(columnValue);
                        break;
                    case 0:
                        clientData.setLux(columnValue);
                        clientDataList.add(clientData);
                        break;
                }

                //System.out.print(columnValue + " " + rsmd.getColumnName(j));
            }

        }
        iotDao.closeAll(null,null,rs);

    }

}
